package com.zzq.aop;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;

@org.aspectj.lang.annotation.Aspect
@Component
public class Aspect {

//    @Around("execution(* com.zzq.service.*.*(..))")
//    public Object AroundComputeTime(ProceedingJoinPoint pjp) throws Throwable { //pjp代表了目标方法
//        //目标方法运行前
//        long startTime = System.nanoTime();
//
//        //目标方法
//        Object result=pjp.proceed();
//
//        //目标方法运行后
//        long endTime = System.nanoTime();
//
//        System.out.println("目标方法:"+pjp.toShortString()+"运行的时间是"+(endTime-startTime)+"纳秒");
//        return result;
//    }
//
//    //Logger对象
//    private Logger logger=Logger.getLogger(Aspect.class);
//
//    //定义切入点，拦截所有controller
//    @Pointcut("execution(public * com.zzq.controller.*.*(..)))")
//    public void allController(){};
//    //前增强
//    @Before("allController()")
//    public void requestLog(){
//        //获取request对象
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        //请求地址、请求方法、ip
//        logger.info("url："+request.getRequestURI());
//        logger.info("http_method："+request.getMethod());
//        logger.info("ip："+request.getRemoteAddr());
//        //遍历请求参数
//        Enumeration<String> paramNames = request.getParameterNames();
//        while (paramNames.hasMoreElements()){
//            String paramName = paramNames.nextElement();
//            String paramVal = request.getParameter(paramName);
//            logger.info(paramName+"："+paramVal);
//        }
//    }
}
